﻿using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;

namespace Nereus
{
    using Power;
    public class NClass
    {
        public string Code
        {
            get; set;
        }

        public string Name
        {
            get; set;
        }

        public string Description
        {
            get; set;
        }

        public List<NMethod> Methods
        {
            get; set;
        }

        private static List<NClass> _allNClass = null;
        public static List<NClass> BuildNClass(string[] assemblyPathArray = null)
        {

            if (_allNClass == null)
            {
                _allNClass = new List<NClass>();
                var nAssemblies = new List<Assembly>();
                if (assemblyPathArray == null || assemblyPathArray.Count() <= 0)
                {
                    var assemblies = new Assembly[] { Assembly.GetEntryAssembly() };
                }
                else
                {
                    foreach (var item in assemblyPathArray)
                    {
                        nAssemblies.Add(AppDomain.CurrentDomain.Load(item));
                    }
                }

                var allTypes = new List<Type>();

                foreach (var item in nAssemblies)
                {
                    allTypes.AddRange(item.GetTypes());
                }
                foreach (var type in allTypes)
                {
                    var nClassAttributes = type.GetCustomAttributes<NClassAttribute>();
                    foreach (var nClassAttribute in nClassAttributes)
                    {
                        var nClass = new NClass()
                        {
                            Code = nClassAttribute.Code,
                            Description = nClassAttribute.Description,
                            Name = nClassAttribute.Name,
                            Methods = new List<NMethod>()
                        };
                        nClass.Methods.AddRange(NMethod.BuildNMethod(type));
                        _allNClass.Add(nClass);
                    }
                }
            }
            return _allNClass;
        }
    }

    public class NClassAttribute : Attribute
    {
        public string Code
        {
            get; set;
        }

        public string Name
        {
            get; set;
        }

        public string Description
        {
            get; set;
        }
    }
}
